package com.thunder.kocho.circle;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.thunder.kocho.R;


/**
 * 作用于圆形图片上的白色光带控件
 * @author Hilex
 * @date 2021-3-4
 */
public class CircleBeltView extends FrameLayout {

    private final CircleBelt mCircleBelt;

    public CircleBeltView(Context context){
        this(context,null);
    }

    public CircleBeltView(Context context, AttributeSet attrs){
        this(context,attrs,0);
    }

    public CircleBeltView(Context context, AttributeSet attrs, int defStyle){
        super(context,attrs,defStyle);
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.CircleBeltView,defStyle,0);
        int res=typedArray.getResourceId(R.styleable.CircleBeltView_circleBeltRes,R.drawable.constellation);
        int mBeltCycle = typedArray.getInt(R.styleable.CircleBeltView_circleBeltCycle, 2000);
        float mBeltWidthPercentage = typedArray.getFloat(R.styleable.CircleBeltView_circleBeltWidthPercentage, 0.24f);
        typedArray.recycle();

        LayoutInflater.from(context).inflate(R.layout.circle_belt,this,true);
        CircleView mImageView = (CircleView) findViewById(R.id.circleView_belt);
        mCircleBelt = (CircleBelt) findViewById(R.id.circleBelt);
        mImageView.setCircleRes(res);
        mCircleBelt.setBeltCycle(mBeltCycle);
        mCircleBelt.setBeltWidthPercentage(mBeltWidthPercentage);
    }

    /**
     * 延迟开启动画
     * @param delay 延迟的毫秒数
     */
    public void postDelayAnimation(int delay){
        if(delay>=0){
            mCircleBelt.postDelayAnimation(delay);
        }
    }

    public void endAnimation(){
        mCircleBelt.endAnimation();
    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        switch(visibility){
            case INVISIBLE:case GONE:
                endAnimation();
                break;

            case VISIBLE:
                break;
        }
    }
}